from typing import Optional, List

from fastapi import Depends, Query, Body
from fastapi_utils.cbv import cbv
from fastapi_utils.inferring_router import InferringRouter
from sqlalchemy.orm import Session

from server.app.cloud_ide.schema.ide_image.request.ide_image_create_request import IdeImageCreateRequest
from server.app.cloud_ide.schema.ide_image.request.ide_image_update_request import IdeImageUpdateRequest
from server.app.cloud_ide.service.ide_image_service import IdeImageService
from server.framework.core.http_schemas import CommonResponseSchema, TableResponseSchema
from server.framework.core.injects import get_db
from server.framework.core.schemas import OptionsSchema

ide_image_api = InferringRouter()


@cbv(ide_image_api)
class IdeImageApi:
    db: Session = Depends(get_db)
    ide_image_service = IdeImageService()

    @ide_image_api.get("/get_ide_image_options", name='获取镜像下拉选项列表', response_model=List[OptionsSchema])
    async def get_ide_image_options(self, registry_id: str = Query(None, description='镜像源id'),
                                    ) -> List[OptionsSchema]:
        results = self.ide_image_service.get_ide_images_by_registry_id(self.db, registry_id)

        option_results: List[OptionsSchema] = []
        for row in results:
            option_results.append(OptionsSchema(value=str(row.id), label=row.name))
        return option_results

    @ide_image_api.get("/get_entity", name='获取镜像')
    async def get_entity(self,
                         id: str = Query(None, description='镜像id')):
        entity = self.ide_image_service.get_ide_image_by_id(self.db, id)
        return entity

    @ide_image_api.get("/list", response_model=TableResponseSchema, name='获取镜像列表')
    async def list(self, name: Optional[str] = Query(None, description='过滤条件:镜像名称'),
                   ide_registry_id: Optional[str] = Query(None, description='过滤条件:镜像源id'),
                   current: Optional[int] = Query(1, description='当前页数'),
                   page_size: Optional[int] = Query(10, description='每页记录的数量',
                                                    alias='pageSize')) -> TableResponseSchema:
        """
        获取IDE镜像列表
        :return:
        """
        items, total = self.ide_image_service.get_ide_image_list(self.db, ide_registry_id, name, page_size, current)
        return TableResponseSchema(data=items, total=total, page=page_size, success=True)

    @ide_image_api.post("/create", response_model=CommonResponseSchema, name='创建镜像')
    async def create(self, data: IdeImageCreateRequest = Body(None, description='镜像创建请求')) -> CommonResponseSchema:
        """
        创建IDE镜像配置
        :return:
        """
        rs = self.ide_image_service.create_ide_image(self.db, data)
        if rs:
            return CommonResponseSchema(data={}, message=f'创建成功', success=True)
        else:
            return CommonResponseSchema(data={}, message=f'创建失败', success=False)

    @ide_image_api.get("/delete", response_model=CommonResponseSchema, name='删除镜像')
    async def delete(self, ids: str = Query(None, description='镜像id，多条记录以逗号分割')) -> CommonResponseSchema:
        """
        删除IDE镜像配置
        :return:
        """
        rs = self.ide_image_service.delete_ide_image(self.db, ids.split(','))
        if rs:
            return CommonResponseSchema(data={}, message=f'删除成功', success=True)
        else:
            return CommonResponseSchema(data={}, message=f'删除失败', success=False)

    @ide_image_api.post("/edit", response_model=CommonResponseSchema, name='编辑镜像')
    async def edit(self, data: IdeImageUpdateRequest = Body(None, description='镜像编辑实体')) -> CommonResponseSchema:
        """
        编辑IDE镜像配置
        :return:
        """
        rs = self.ide_image_service.update_ide_image(self.db, data)
        if rs:
            return CommonResponseSchema(data={}, message=f'编辑成功', success=True)
        else:
            return CommonResponseSchema(data={}, message=f'编辑失败', success=False)

    @ide_image_api.get("/show_logs", name='构建镜像', response_model=CommonResponseSchema)
    async def show_logs(self, id: str = Query(None, description='镜像ID')):
        result = self.ide_image_service.get_ide_build_log(self.db, id)
        return CommonResponseSchema(data=result, message='')

    @ide_image_api.get("/build", name='构建镜像', response_model=CommonResponseSchema)
    async def build(self, id: str = Query(None, description='镜像ID')):
        rs = self.ide_image_service.build_ide_image(self.db, id)
        if rs:
            return CommonResponseSchema(message='构建成功', data={}, success=True)
        else:
            return CommonResponseSchema(message='构建失败', data={}, success=False)
